package cn.gson.oasys.common.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 时间工具类
 * 
 * @author zhuitian
 *
 */
public class DateUtil {
	private static final Logger log = LoggerFactory.getLogger(DateUtil.class);

	public static String getStartWeekTimeOfNow() {
		LocalDate now = LocalDate.now();
		DayOfWeek dayOfWeek = now.getDayOfWeek();
		int separateDay = separateToMONDAYOfWeek(dayOfWeek) + 1;
		
		Date date = getDateBefore(new Date(), separateDay);
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		return df.format(date) + ":23:59:59";
	}
	
	public static String getEndWeekTimeOfNow() {
		LocalDate now = LocalDate.now();
		DayOfWeek dayOfWeek = now.getDayOfWeek();
		int separateDay = separateToSUNDAYOfWeek(dayOfWeek);
		
		Date date = getDateAfter(new Date(), separateDay);
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		return df.format(date) + ":23:59:59";
	}
	
	public static String getWeekStartTimeOfNowString() {
		Date date = getWeekStartTimeOfNow();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		return df.format(date);
	}
	
	public static Date getWeekStartTimeOfNow() {
		LocalDate now = LocalDate.now();
		DayOfWeek dayOfWeek = now.getDayOfWeek();
		int separateDay = separateToMONDAYOfWeek(dayOfWeek);

		return getDateBefore(new Date(), separateDay);
	}
	
	public static String getWeekEndTimeOfNowString() {
		Date date = getWeekEndTimeOfNow();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		return df.format(date);
	}
	
	public static Date getWeekEndTimeOfNow() {
		LocalDate now = LocalDate.now();
		DayOfWeek dayOfWeek = now.getDayOfWeek();
		int separateDay = DayOfWeek.SUNDAY.getValue() - dayOfWeek.getValue();

		return getDateAfter(new Date(), separateDay);
	}

	private static int separateToMONDAYOfWeek(DayOfWeek dayOfWeek) {
		int result = 0;
		switch (dayOfWeek) {
		case MONDAY:
			result = 0;
			break;
		case TUESDAY:
			result = 1;
			break;
		case WEDNESDAY:
			result = 2;
			break;
		case THURSDAY:
			result = 3;
			break;
		case FRIDAY:
			result = 4;
			break;
		case SATURDAY:
			result = 5;
			break;
		case SUNDAY:
			result = 6;
			break;
		default:
			break;
		}
		return result;
	}
	
	private static int separateToSUNDAYOfWeek(DayOfWeek dayOfWeek) {
		int result = 0;
		switch (dayOfWeek) {
		case MONDAY:
			result = 6;
			break;
		case TUESDAY:
			result = 5;
			break;
		case WEDNESDAY:
			result = 4;
			break;
		case THURSDAY:
			result = 3;
			break;
		case FRIDAY:
			result = 2;
			break;
		case SATURDAY:
			result = 1;
			break;
		case SUNDAY:
			result = 0;
			break;
		default:
			break;
		}
		return result;
	}

	/** 
	* 得到几天前的时间 
	* @param day
	* @return 
	*/
	public static Date getDateBefore(Date d,int day){  
		Calendar now =Calendar.getInstance();  
		now.setTime(d);  
		now.set(Calendar.DATE,now.get(Calendar.DATE)-day);  
		return now.getTime();  
	}

	/**
	 * 得到几天后的时间
	 * 
	 * @param day
	 * @return
	 */
	public static Date getDateAfter(Date d, int day) {
		Calendar now = Calendar.getInstance();
		now.setTime(d);
		now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
		return now.getTime();
	}
	
	public static void main(String[] args) {
		System.out.println(LocalDate.now().getDayOfWeek());
		System.out.println(getStartWeekTimeOfNow());
		System.out.println(getEndWeekTimeOfNow());

	}
}
